//==============================================================================
//
//             Copyright (C) 2002-2005  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2006-08-21 16:45:39 $ $Revision: 1.7 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DText.h"
#include "ofc/DData.h"

#include "DInc.h"
#include "DTest.h"

//-DataTypes-------------------------------------------------------------------

void DText_test()
{
  DText *string = [DText alloc];
  DText *copy;
  DText *sub;
  char   ch;
  int    i;
  
  STARTTEST();

  [[string init] format :"Hello world %d",4];
  
  TEST([string ccompare :"hello world 4"] != 0);
  TEST([string ccompare :"Hello world 4"] == 0);
  TEST([string icompare :"hello world 4"] == 0);
  TEST([string icompare :"hello world 5"] != 0);

  ch = [string pop];

  TEST((ch == '4') && ([string ccompare :"Hello world "] == 0) && ([string length] == 12));

  [string free];
  string = [DText alloc];
  
  string = [[string init :"Hello"] center :7];

  TEST([string ccompare :" Hello "] == 0);

  [string delete :6];
  [string put    :4 :'?'];
  ch = [string get :5];

  TEST((ch == 'o') && ([string ccompare :" Hel?o"] == 0));

  copy = [string copy];
  
  // Copy test
  TEST(string != copy);
  TEST([string cstring] != [copy cstring]);
  TEST([string compare :copy] == 0);

  [copy free];

  // Modifiers test
  TEST([[string append :" DAAG"] ccompare :" Hel?o DAAG"] == 0); 
  TEST([[string swapcase] ccompare :" hEL?O daag"] == 0);
  TEST([[string lower] ccompare :" hel?o daag"] == 0);
  TEST([[string prepend :"test"] ccompare :"test hel?o daag"] == 0);
  TEST([[string capitalize] ccompare :"Test hel?o daag"] == 0);
  TEST([[string capwords] ccompare :"Test Hel?o Daag"] == 0);

  [string free];

  // Append format tests
  string = [DText new];
  
  [string set :"Start:10"];
  
  for (i = 20; i < 60; i+=10)
  {
    [string appendFormat :"+%d", i];
  }
  [string append :"=150"];
  
  TEST([string ccompare :"Start:10+20+30+40+50=150"] == 0);
  
  [string free];
  
  // Range tests  
  string = [DText alloc];
  
  [string init];
  [string set :"This is a long text text" :0 :18];

  TEST([string ccompare :"This is a long text"] == 0);

  TEST([[string insert :15 :18 :"sentence"] ccompare :"This is a long sentence"] == 0);
  TEST([[string insert :0 :3 :"1."] ccompare :"1. is a long sentence"] == 0); 
  
  TEST([[string get :1 :-2] ccompare :". is a long sentenc"] == 0); 

  TEST([[string delete :0 :1] ccompare :" is a long sentence"] == 0);
  TEST([[string delete :11 :18] ccompare :" is a long "] == 0);

  TEST([[string insert :3 :3 :' ' :3] ccompare :" is   a long "] == 0); // insert with delete
  TEST([[string insert :1 :2 :'?' :2] ccompare :" ??   a long "] == 0);
  TEST([[string insert :2 :1 :'!' :1] ccompare :" ?!?   a long "] == 0); // insert without delete

  [string set :""];

  TEST([string isEof]);
  TEST([string writeLine :"Hallo"]);
  TEST([string ccompare :"Hallo\n"] == 0);
  TEST([string isEof]);
  TEST([string tell] == 6);
  TEST([string writeChar :'1']);
  TEST([string ccompare :"Hallo\n1"] == 0);
  TEST([string writeText :";"]);
  TEST([string ccompare :"Hallo\n1;"] == 0);

  TEST([string seek :3 :2]);
  TEST([string tell] == 5);
  TEST([string readChar] == '\n');
  TEST([string seek :1 :0]);
  TEST([[string readLine] ccompare :"allo"] == 0);
  TEST([[string readText :1] ccompare :"1"] == 0);
  TEST([string seek :0 :0]);
  TEST([[string readText] ccompare :"Hallo\n1;"] == 0);

  [string set :"  This is a         test for the multi line column method with a veryveryveryverylongword.\n  This is the second part of the line."];
  
  TEST([[string column :15] ccompare :"  This is a\ntest for the multi line\ncolumn method\nwith a\nveryveryveryver\nylongword.\n  This is the\nsecond part of\nthe line."]);

  [string set :"abc: def:"];
  
  TEST([string split :'='] == nil);
  
  sub = [string split :':'];
  
  TEST(sub != nil);
  TEST([sub    ccompare :"abc"] == 0);
  TEST([string ccompare :" def:"] == 0);
  [sub free];
  
  sub = [string split :':'];
  
  TEST(sub != nil);
  TEST([sub    ccompare :" def"] == 0);
  TEST([string length] == 0);
  [sub free];

  [string set :":abc"];
  
  sub = [string split :':'];
  TEST(sub != nil);
  TEST([sub length] == 0);
  TEST([string ccompare :"abc"] == 0);
  [sub free];

  [string set :' ' :0];
  TEST([string length] == 0);
  TEST([string ccompare :""] == 0);
  
  [string set :'A' :4];
  TEST([string ccompare :"AAAA"] == 0);

  [string set :"This is a test of the scanText method"];
  sub = [string scanText :" atm" :&ch]; 
  TEST(sub != NULL);
  TEST([sub ccompare :"This"] == 0); 
  TEST(ch == ' ');
  [sub free];
  
  [string set :"This is a test of the scanText method"];
  sub = [string scanText :"mta" :&ch];
  TEST(sub != NULL);
  TEST([sub ccompare :"This is "] == 0);
  TEST(ch == 'a');
  [sub free];
  
  [string set :"This is a test of the scanText method"];
  sub = [string scanText :"bg" :&ch];
  TEST(sub == NULL);
  
  [string free];
  
  STOPTEST();
}

